package edu.iyte.ceng316.ui.phonebook.left;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;

import edu.iyte.ceng316.model.PhoneGroup;
import edu.iyte.ceng316.model.data.IPhoneBookModel;
import edu.iyte.ceng316.model.data.PhoneBookManager;
import edu.iyte.ceng316.model.data.PhoneBookSingleton;
import edu.iyte.ceng316.ui.phonebook.dialog.PhoneBookDialog;
import edu.iyte.ceng316.ui.phonebook.mediator.IPhoneBookMediator;


public class LeftPanel extends JPanel implements ActionListener{
	
	JButton newDepartmentButton;
	JButton editDepartmentButton;
	JButton deleteDepartmentButton;
	
	JPanel groupToolbar;
    
	JList departmentList;
    
    IPhoneBookMediator iPhoneBookMediator;
    
	/**
	 * Create the panel.
	 */
	public LeftPanel() {
		
		groupToolbar = new JPanel();
		FlowLayout flowLayout = (FlowLayout) groupToolbar.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		
		newDepartmentButton = new JButton("New");
		newDepartmentButton.addActionListener(this);
		newDepartmentButton.setEnabled(true);
		groupToolbar.add(newDepartmentButton);
		
		editDepartmentButton = new JButton("Edit");
		editDepartmentButton.addActionListener(this);
		editDepartmentButton.setEnabled(false);
		groupToolbar.add(editDepartmentButton);
		
		deleteDepartmentButton = new JButton("Delete");
		deleteDepartmentButton.addActionListener(this);
		deleteDepartmentButton.setEnabled(false);
		groupToolbar.add(deleteDepartmentButton);
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0};
		setLayout(gridBagLayout);

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(0, 0, 5, 5);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridx = 0;
		gbc.gridy = 0;
		add(groupToolbar,gbc);
			
		departmentList = new JList();
	
		GridBagConstraints gbc_textPane = new GridBagConstraints();
		gbc_textPane.insets = new Insets(0, 0, 5, 5);
		gbc_textPane.fill = GridBagConstraints.BOTH;
		gbc_textPane.gridx = 0;
		gbc_textPane.gridy = 1;
		add(departmentList, gbc_textPane);
	}


	public JButton getNewDepartmentButton() {
		return newDepartmentButton;
	}

	public JButton getEditDepartmentButton() {
		return editDepartmentButton;
	}

	public JButton getDeleteDepartmentButton() {
		return deleteDepartmentButton;
	}

	public JList getDepartmentList() {
		return departmentList;
	}
	
	public void setIPhoneBookMediator(IPhoneBookMediator iPhoneBookMediator) {
		this.iPhoneBookMediator = iPhoneBookMediator;
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==newDepartmentButton){
			createDepartment();
	     }
		if(e.getSource()==editDepartmentButton){
			editDepartment();
		}
		if(e.getSource()==deleteDepartmentButton){
			deleteDepartment();
		}
    }
	
	public void createDepartment(){
		iPhoneBookMediator.createDepartment();
	}
	
	public void editDepartment(){
		iPhoneBookMediator.editDepartment();
	}
	
	public void deleteDepartment(){
		iPhoneBookMediator.deleteDepartment();
	}
	


}
